<?php
class Manage_comments extends CI_Model {

    function __construct(){
        parent::__construct();
        $this->load->model('get_profile_infos');
        $this->load->model('get_time');
    }


    function addComment($type,$id,$comment,$user_id,$timeAdded,$timestamp) {
        $profile_pic = $this->get_profile_infos->getProfilePicture($user_id);
        $user_name = $this->get_profile_infos->getname($user_id);
        $data = array(
          'type' => $type,
          'id' => $id,
          'comment' => $comment,
          'user_id' => $user_id,
          'added' => $timeAdded,
          'timestamp' => $timestamp,
          'user_profile_pic_thumb' => $profile_pic,
          'user_name' => $user_name
        );
        $insert = $this->mongo_db->insert('comments_'.$type, $data);
        if($insert){
            return $this->getSpecComments($type,$id,$timestamp,$user_id);
        }else{
            return $insert;
        }
        
    }
    function getcomments($type,$id){
        
        $data = array(
          'id' => $id
        );
        $this->mongo_db->where($data);
        $query = $this->mongo_db->order_by(array('timestamp' => 'DESC'))->get('comments_'.$type);        
        foreach ($query as $key => $value){
            $query[$key]["timestamp"] = $this->get_time->ago(time(),$value['timestamp']);
        }
        return $query;
    }
    function getSpecComments($type,$id,$timestamp,$user_id){
        $data = array(
          'id' => $id,
          'timestamp' => $timestamp, 
          'user_id' => $user_id,
        );
        $this->mongo_db->where($data);
        $query = $this->mongo_db->get('comments_'.$type);
        return $query[0];
    }
}
?>
